package com.fansl.allround.oss.controller;

import com.alibaba.fastjson.JSONObject;
import com.fansl.allround.common.core.util.R;
import com.fansl.allround.oss.service.AliyunOssService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author fansl
 * @Description: 阿里云oss
 * @date 2019/9/2 17:18
 */
@RestController
@Slf4j
@RequestMapping("/aliyun/oss")
@AllArgsConstructor
public class AliyunOssController {
    private final AliyunOssService aliyunOssService;

    /**
     * 获取上传文件的签名url
     * @param objectName 文件名
     * @return
     */
    @GetMapping("/signature")
    public R getSignatureUrl(@RequestParam(required = false)String objectName){
        return new R(aliyunOssService.getSignatureUrl(objectName));
    }

    @PostMapping("/upload")
    public R uploadFile(MultipartFile file){
        return new R(aliyunOssService.uploadFile(file));
    }

    /**
     * 上传成功回调方法
     * @param request
     * @return
     */
    @PostMapping("/callback")
    @ResponseBody
    public R callback(HttpServletRequest request) {
        JSONObject data = new JSONObject();
        data.put("size", request.getParameter("size"));
        data.put("mimeType", request.getParameter("mimeType"));
        data.put("width", request.getParameter("width"));
        data.put("height", request.getParameter("height"));
        return new R(data);
    }
}
